﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WWX.Admin.Web.Common;

namespace WWX.Admin.Web
{
    public static class ControllerExtensions
    {
        public static JsonResult CreateModelStateErrorJsonResult(this Controller controller)
        {
            List<string> errorMessages = new List<string>();

            foreach (var item in controller.ModelState)
            {
                foreach (var item2 in item.Value.Errors)
                {
                    errorMessages.Add(item2.ErrorMessage);
                }
            }

            return JsonResultHelper.CreateRichJsonResult(false, string.Join(".", errorMessages));
        }


        public static JsonResult CreateRichJsonResult(this Controller controller, bool isSuccess = true, string message = "", object data = null)
        {
            return JsonResultHelper.CreateRichJsonResult(isSuccess, message, data);
        }

        public static JsonResult CreateJsonResult(this Controller controller, object data)
        {
            return JsonResultHelper.CreateJsonResult(data);
        }
    }
}